package com.qk.management.task;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.qk.common.config.AliYunOSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/*
 *  @author SHCJ
 *  @created 2025/10/12 23:13
 *  @description
 */
@Component
public class OssDelete {
    @Autowired
    private AliYunOSSProperties aliYunOSSProperties ;
    public void deleteFile(String fileName) {
        // 创建OSSClient实例
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = null;
        try {
            ossClient = OSSClientBuilder.create()
                    .endpoint(aliYunOSSProperties.getEndpoint())
                    .credentialsProvider(new EnvironmentVariableCredentialsProvider())
                    .clientConfiguration(clientBuilderConfiguration)
                    .region(aliYunOSSProperties.getRegion())
                    .build();
            ossClient.deleteObject(aliYunOSSProperties.getBucketName(), fileName);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
